<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="消息标题" prop="messageTitle">
        <el-input
          v-model="queryParams.messageTitle"
          placeholder="请输入消息标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送人" prop="fromName">
        <el-input
          v-model="queryParams.fromName"
          placeholder="请输入发送人名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="消息状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="阅读状态" prop="readStatus">
        <el-select
          v-model="queryParams.readStatus"
          placeholder="阅读状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in readStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:message:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:message:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:message:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="changeMessageReadStatus0"
          v-hasPermi="['system:message:remove']"
        >未读
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="changeMessageReadStatus1"
          v-hasPermi="['system:message:remove']"
        >已读
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column align="center" label="消息编号" prop="messageId" width="80"/>
      <el-table-column align="center" label="标题" :show-overflow-tooltip="true" prop="messageTitle" width="150"/>
      <el-table-column align="center" label="内容" :show-overflow-tooltip="true" prop="messageContent" width="150"/>
      <!--      <el-table-column align="center" label="发送人编号" prop="fromId" :show-overflow-tooltip="true" width="90"/>
            <el-table-column  align="center" label="接收人编号" prop="toId" :show-overflow-tooltip="true" width="90"/>-->
      <el-table-column align="center" label="发送人" :show-overflow-tooltip="true" prop="fromName" width="80"/>
      <el-table-column align="center" label="接收人" :show-overflow-tooltip="true" prop="toName" width="80"/>
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="阅读状态"
        align="center"
        prop="readStatus"
        :formatter="statusFormat"
        width="80"
      />
      <el-table-column label="发送时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:message:edit']"
          >修改
          </el-button>
          <el-button
            v-if="scope.row.readStatus=='0'"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="changeMessageReadStatus('0',scope.row)"
            v-hasPermi="['system:message:edit']"
          >已读
          </el-button>
          <el-button
            v-if="scope.row.readStatus=='1'"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="changeMessageReadStatus('1',scope.row)"
            v-hasPermi="['system:message:edit']"
          >未读
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消息配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="发送人" prop="fromName">
              <el-input v-model="form.fromName" placeholder="请输入发送人名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收人" prop="toName">
              <el-input v-model="form.toName" placeholder="请输入接收人名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="消息标题" prop="messageTitle">
              <el-input v-model="form.messageTitle" placeholder="请输入消息标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="阅读状态">
              <el-radio-group v-model="form.readStatus">
                <el-radio
                  v-for="dict in readStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <el-input v-model="form.messageContent" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMessage,
  addMessage,
  getFromMessage,
  getMessage,
  getToMessage,
  updateMessage,
  updateMessageReadStatus,
  delMessage,
  delMessageReadStatus0,delMessageReadStatus1, updateMessageStatus
} from "@/api/system/message";

export default {
  name: "Message",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      readStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageTitle: undefined,
        fromName: undefined,
        toName: undefined,
        fromId: undefined,
        toId: undefined,
        readStatus: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fromName: [
          {required: true, message: "发送人名称不能为空", trigger: "blur"}
        ],
        toName: [
          {required: true, message: "接收人名称不能为空", trigger: "blur"}
        ],
        messageTitle: [
          {required: true, message: "接收人名称不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_message_read").then(response => {
      this.readStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询消息列表 */
    getList() {
      this.loading = true;
      listMessage(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.messageList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 新闻状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.readStatusOptions, row.readStatus);
    },
    // 消息状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.messageTitle + '"消息吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updateMessageStatus(row.messageId, row.status);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    //阅读状态已读
    changeMessageReadStatus1(){
      const messageIds =  this.ids;
      this.$confirm('是否确认将消息编号为"' + messageIds + '"的数据项标记为已读?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delMessageReadStatus1(messageIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("标记已读成功");
      })
    },
    //阅读状态未读
    changeMessageReadStatus0(){
      const messageIds =  this.ids;
      this.$confirm('是否确认将消息编号为"' + messageIds + '"的数据项标记为已读?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delMessageReadStatus0(messageIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("标记已读成功");
      })
    },
    //阅读状态修改
    changeMessageReadStatus(btn, row) {
      if (btn == '0') {
        this.$confirm('是否确认将消息编号为"' + row.messageId + '"的数据项标记为已读?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return updateMessageReadStatus(row.messageId, '1');
        }).then(() => {
          this.getList();
          this.msgSuccess("标记已读成功");
        })
      } else if (btn == '1') {
        this.$confirm('是否确认删除消息编号为"' + row.messageId + '"的数据项标记为未读?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return updateMessageReadStatus(row.messageId, '0');
        }).then(() => {
          this.getList();
          this.msgSuccess("标记未读成功");
        })
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        messageId: undefined,
        fromId: undefined,
        toId: undefined,
        fromName: undefined,
        toName: undefined,
        messageTitle: undefined,
        messageContent: undefined,
        payload: undefined,
        status: "0",
        readStatus: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.messageId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const messageId = row.messageId || this.ids
      getMessage(messageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.messageId != undefined) {
            updateMessage(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const messageIds = row.messageId || this.ids;
      this.$confirm('是否确认删除消息编号为"' + messageIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delMessage(messageIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    }
  }
}
</script>

<style scoped>

</style>
